﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Diagnostics;

namespace AlgoLogicServer
{
    class DBManager
    {
        // 1.Properties
        public String TeamName { get; set; }
        public DateTime Date { get; set; }

        //SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=AlgoLogicDatabase;Persist Security Info=True;User ID = sa; Password = imsa");
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=AlgoLogicDB;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();

        public void InsertTeamName()
        {
            try
            {
                conn.Open();
                cmd.Connection = conn;

                cmd.CommandText = "INSERT INTO Competitor (TeamName, Date)values (@TeamName, @Date)";
                cmd.Parameters.AddWithValue("@TeamName", TeamName);
                cmd.Parameters.AddWithValue("@Date", Date);
                cmd.ExecuteNonQuery();
                Debug.WriteLine("Sql Command Completed");
            }
            catch (SqlException ex)
            {
                // IF there is an error in the database
                throw ex;
            }
            finally
            {
                // No matter what, run this.
                conn.Close();
            }
        }

        public bool RetreiveTeamName()
        {
            bool result = false;
            try
            {
                conn.Open();
                cmd.Connection = conn;

                //execute sql command and READ values
                cmd.CommandText = "SELECT * FROM Competitor WHERE TeamName=@TeamName";
                cmd.Parameters.AddWithValue("@TeamName", TeamName);
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read()) // Read each row
                {
                    TeamName = dr["TeamName"].ToString();
                    result = true;
                }
                dr.Close();
            }
            catch (SqlException ex)
            {
                // IF there is an error in the database
                throw ex;
            }
            finally
            {
                // No matter what, run this.
                conn.Close();
            }
            return result;
        }
    }
}